﻿using BZOrderPrint.BLL.Helper;
using BZOrderPrint.DAL;
using BZOrderPrint.DAL.Entity;
using BZOrderPrint.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BZOrderPrint
{
    public partial class FrmShopManage : Form
    {
        private PageModel PageModel { get; set; }

        private BZContext context { get; set; }

        public FrmShopManage()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            context = new BZContext(ConfigHelper.ConnString);
        }

        private ShopPageModel GetShopPageModel(int PageIndex)
        {
            ShopPageModel model = new ShopPageModel();
            Expression<Func<BzShopEntity, bool>> predicate = x => true;
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                predicate = predicate.And(x => x.ShopCode.Contains(textBox1.Text));
            }
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                predicate = predicate.And(x => x.ShopName.Contains(textBox2.Text));
            }
            var count = context.bzShopEntities.Count(predicate.Compile());
            if (count > 0)
            {
                model.PageSize = 30;
                model.TotalRows = count;
                model.TotalPage = count % 30 == 0 ? count / 30 : count / 30 + 1;
                model.PageIndex = PageIndex;
                model.bzShopEntities = context.bzShopEntities.Where(predicate.Compile()).Skip((PageIndex - 1) * 30).Take(30).ToList();
            }
            else
            {
                model.TotalRows = 0;
                model.TotalPage = 0;
                model.PageIndex = PageIndex;
                model.PageSize = 30;
            }
            return model;
        }

        private void DataGirdViewInit(ShopPageModel model)
        {
            #region 初始化
            dataGridView1.Columns.Clear();
            var data = new DataTable();
            var dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "ID",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "网点编码",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "网点名称",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "所在省份",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "所在城市",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "所在区",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "所在街道",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "具体地址",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "邮编",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "电话",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "店长",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "网点类型",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);

            #endregion 初始化

            foreach (var item in model.bzShopEntities)
            {
                var dataRow = data.NewRow();
                dataRow["ID"] = item.Id;
                dataRow["网点编码"] = item.ShopCode;
                dataRow["网点名称"] = item.ShopName;
                dataRow["所在省份"] = item.Province;
                dataRow["所在城市"] = item.City;
                dataRow["所在区"] = item.Area;
                dataRow["所在街道"] = item.Street;
                dataRow["具体地址"] = item.Address;
                dataRow["邮编"] = item.Postcode;
                dataRow["电话"] = item.Phone;
                dataRow["店长"] = item.ShopAdmin;
                dataRow["网点类型"] = ((AppEnum.ShopType) item.ShopType).GetDescription();
                data.Rows.Add(dataRow);
            }
            //DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn();
            //checkColum.HeaderText = "选择";
            //checkColum.Name = "IsChecked";
            //checkColum.DataPropertyName = "IsChecked";
            //checkColum.ReadOnly = false;
            //checkColum.TrueValue = true;
            //checkColum.FalseValue = false;
            //checkColum.Width = 40;
            //dataGridView1.Columns.Insert(0, checkColum);
            DataGridViewButtonColumn buttonColum = new DataGridViewButtonColumn();
            buttonColum.Text = "查看";
            buttonColum.Name = "查看";
            buttonColum.ToolTipText = "查看";
            buttonColum.ReadOnly = false;
            buttonColum.Width = 40;
            dataGridView1.Columns.Insert(0, buttonColum);
            dataGridView1.DataSource = data;
            dataGridView1.ReadOnly = false;
            dataGridView1.Columns[1].Visible = false;
            for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            pageIndex.Text = model.PageIndex.ToString();
        }

        private void btnSreach_Click(object sender, EventArgs e)
        {
            int i = 1;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }

            DataGirdViewInit(GetShopPageModel(i));
        }

        private void pageIndex_TextChanged(object sender, EventArgs e)
        {
            if (!int.TryParse(pageIndex.Text, out int index))
            {
                MessageBox.Show("请输入数字！");
                return;
            }
            else if (index < 1)
            {
                MessageBox.Show("请输入大于0的数字！");
                return;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }

            DataGirdViewInit(GetShopPageModel(++i));
        }

        private void FrmShopManage_Load(object sender, EventArgs e)
        {
            DataGirdViewInit(GetShopPageModel(1));
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(pageIndex.Text, out i))
            {
                MessageBox.Show("页码错误");
                return;
            }
            if (--i == 0)
                return;
            DataGirdViewInit(GetShopPageModel(--i));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FrmShopModify frmShopModify = new FrmShopModify(new BzShopEntity());
            frmShopModify.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //var id = 0;

            //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            //{
            //    if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
            //    {
            //        if (id > 0)
            //        {
            //            MessageBox.Show("请选择一条数据修改!");
            //            return;
            //        }
            //        id = int.Parse(dataGridView1.Rows[i].Cells["ID"].Value.ToString());
            //        using (var db = new BZContext(ConfigHelper.ConnString))
            //        {
            //            var shop = db.bzShopEntities.FirstOrDefault(x => x.Id.Equals(id));
            //            FrmShopModify frmShopModify = new FrmShopModify(shop);
            //            frmShopModify.ShowDialog();
            //        }
            //    }
            //}
            //if (id == 0)
            //{
            //    MessageBox.Show("请选择一条数据修改!");
            //    return;
            //}
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
            if (column is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                int id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                var shop = context.bzShopEntities.FirstOrDefault(x => x.Id.Equals(id));
                FrmShopModify frmShopModify = new FrmShopModify(shop);
                frmShopModify.ShowDialog();
            }
        }
    }
}